define(['jquery', 'global', 'module/util'], function($, global, util){
var $this = {
	init : function(){
		this.dictNames = [];
		this.loadShowText = '加载中...';
		this.loadSelectDict();
	},
	initDictNames : function() {
		$("select").each(function() {
			if($(this).attr("dict")) {
				$this.dictNames.push($(this).attr("dict"));
			}
			if($(this).find("option:first").length == 0) {
				$(this).html('<option value="">'+ ($(this).attr("title") || '') + $this.loadShowText +'</option>');
			}
		});
		return $this.dictNames;
	},
	
	loadSelectDict : function(rootId) {
		if($this.dictNames.length == 0) $this.initDictNames(); 
		if(!$this.dictNames || $this.dictNames.length == 0 || $.isEmptyObject($this.dictNames)) return;
		global.doInitDictData({//加载select数据字典
			data : $this.dictNames,
			success : function() {
				$this.doLoadSelects(rootId);
			}
		});	
	},
	doLoadSelects : function(rootId) {
		var parentNode = null;
		if(rootId) {
			parentNode = $("#" + rootId);
		} else {
			parentNode = $(document.body);
		}
		parentNode.find("select").each(function() {
			$this.loadOne(this);
		});
	},
	loadOne : function($select) {
		var dictName = $($select).attr("dict");
		var dict = global.dict[dictName];
		if(!dict || $.isEmptyObject(dict)) {
//			global.doInitDictData({
//				data : dictName,
//				success : function() {
//					$this.loadOne($select);
//				}
//			});
		} else {
			var html = '', title = '', value = '', options = $($select).find("option:first"), prefix = $($select).attr("prefix") || '';
			if(options && options.length > 0) {
				value = options[0].value;
				title = prefix + ($($select).attr("title") || (options[0].text.replace($this.loadShowText, "")));
			} else {
				title = prefix + ($($select).attr("title") || '');
			}
			html += '<option selected value="' + value + '">' + (title.length==0 ? "选择" : title) +'</option>';
			for(var value in dict) {
				html += '<option value="'+ value +'">'+ dict[value] +'</option>';
			}
			$($select).html(html);
			$($select).val($($select).attr("default-select") || value);
		}
	}
}
$this.init();
return $this;
});